package com.zhanghao.sdk.service;

import android.content.Context;
import android.util.JsonReader;

import com.zhanghao.sdk.network.ZHHttpArgs;
import com.zhanghao.sdk.network.ZHHttp;
import com.zhanghao.sdk.callback.IZHHTTPCallback;
import com.zhanghao.sdk.network.IZHHttpInterceptor;
import com.zhanghao.sdk.constant.SERVER_HTTP;
import com.zhanghao.sdk.SDKManager;
import com.zhanghao.sdk.util.UserAgentUtils;

import org.json.JSONObject;

public class BaseServiceImpl {
    private static final String HOST = SERVER_HTTP.HOST;
    private Context mContext;
    BaseServiceImpl(){
        mContext = SDKManager.getInstance().getMainContext();
        // 设置主要的上下文,ui操作什么的,可能会用到;
        ZHHttp.getInstance().setMainContext(mContext);
        // 设置拦截器
        ZHHttp.getInstance().setInterceptor(new IZHHttpInterceptor() {

            @Override
            public ZHHttpArgs onConfig(ZHHttpArgs args) {
                args.getHeaders().put("User-Agent", UserAgentUtils.getUserAgent(mContext));
                return args;
            }

            @Override
            public boolean onResponse(JsonReader json) {
                return false;
            }

            @Override
            public void onFail(ZHHttpArgs args, String response) {

            }

            @Override
            public void onError(ZHHttpArgs args, String message) {

            }
        });
    }

    protected void request(ZHHttpArgs httpArgs, final String callbackFlag) {
        ZHHttp.getInstance().request(httpArgs, new IZHHTTPCallback() {
            @Override
            public void onResponse(JSONObject data) {
                callback(callbackFlag,data);
            }
        });
    }

    protected void callback(String flag, JSONObject data) {

    }
}
